package com.example.testdemo.kinetic.sprite.tank.player;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.kinetic.interfaces.IOnDestroyListener;

public class PlayerTank1 extends PlayerBaseTank {

	public static final String BASE_NAME = PlayerBaseTank.BASE_NAME + " - "
	        + PlayerTank1.class.getSimpleName();

	public interface Animated {
		public static final int WALK_START = 0;
		public static final int WALK_END = 1;
		public static final int BOMB_START = 3;
		public static final int BOMB_END = 5;
		public static final int FIRE_START = 6;
		public static final int FIRE_END = 8;
	}

	public static final float FACE_UP = 0;
	public static final float FACE_DOWN = 180;
	public static final float FACE_RIGHT = 90;
	public static final float FACE_LEFT = 270;

	private boolean mIsAnimationCancelable = true;

	public PlayerTank1(int whichScene, String pName, float pX, float pY,
	        ITiledTextureRegion pTiledTextureRegion, TankGameActivity pActivity) {
		super(whichScene, pName, pX, pY, pTiledTextureRegion, pActivity);
	}

	@Override
	public void walkingAnimation() {
		if (mIsAnimationCancelable) {
			animate(new long[] { 100, 100 }, Animated.WALK_START,
			        Animated.WALK_END, true);
		}
	}

	@Override
	protected void fireAnimation() {
		if (mIsAnimationCancelable) {
			animate(new long[] { 20, 20, 30 }, Animated.FIRE_START,
			        Animated.FIRE_END, false,
			        new AnimatedSprite.IAnimationListener() {
				        @Override
				        public void onAnimationStarted(
				                AnimatedSprite pAnimatedSprite,
				                int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFrameChanged(
				                AnimatedSprite pAnimatedSprite,
				                int pOldFrameIndex, int pNewFrameIndex) {
				        }

				        @Override
				        public void onAnimationLoopFinished(
				                AnimatedSprite pAnimatedSprite,
				                int pRemainingLoopCount, int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFinished(
				                AnimatedSprite pAnimatedSprite) {
					        walkingAnimation();
				        }
			        });
		}
	}

	@Override
	protected void destroyedAnimation(final IOnDestroyListener listener) {
		if (mIsAnimationCancelable) {
			mIsAnimationCancelable = false;
			animate(new long[] { 50, 50, 50 }, Animated.BOMB_START,
			        Animated.BOMB_END, false,
			        new AnimatedSprite.IAnimationListener() {
				        @Override
				        public void onAnimationStarted(
				                AnimatedSprite pAnimatedSprite,
				                int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFrameChanged(
				                AnimatedSprite pAnimatedSprite,
				                int pOldFrameIndex, int pNewFrameIndex) {
				        }

				        @Override
				        public void onAnimationLoopFinished(
				                AnimatedSprite pAnimatedSprite,
				                int pRemainingLoopCount, int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFinished(
				                AnimatedSprite pAnimatedSprite) {
					        setVisible(false);
					        mIsAnimationCancelable = true;
					        if (listener != null) {
						        listener.onDestroy(PlayerTank1.this);
					        }
				        }
			        });
		}
	}

	@Override
	public void free() {
		mActivity.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {
				mOnScene.detachChild(PlayerTank1.this);
				dispose();
			}
		});
	}

}
